﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;

namespace Clipping.SearchEnginePageParsers
{
    public class SinaNewsParser : ISearchEnginePageParse
    {
        public void Parse(string html, out List<Infrastructure.Models.News> news, out string nextPageUrl)
        {
            news = new List<News>();
            nextPageUrl = null;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            var liNav = doc.CreateNavigator();
            var divSearchResult = liNav.Select("//div[@class='box-result clearfix']");

            while (divSearchResult.MoveNext())
            {
                News n = new News();

                var li = divSearchResult.Current;
                //取标题和链接
                var linkNode = li.SelectSingleNode("descendant::h2/a");
                if (linkNode != null)
                {
                    n.Title = linkNode.Value;
                    n.Url = linkNode.GetAttribute("href", "");
                }

                //取时间
                var dtNode = li.SelectSingleNode("descendant::p[@class='fgray_time']");

                if (dtNode == null)
                    dtNode = li.SelectSingleNode("descendant::span[@class='fgray_time']");

                if (dtNode != null)
                {
                    string sourceAndPubDate = dtNode.Value;
                    if (sourceAndPubDate != null)
                    {
                        var dtMatch = Regex.Match(sourceAndPubDate, @"\d{4}-\d{1,2}-\d{1,2}\s+\d{2}:\d{2}:\d{2}");
                        if (dtMatch.Success)
                        {
                            n.PubDate = dtMatch.Value;
                            n.Media = sourceAndPubDate.Replace(n.PubDate, "").Trim();
                        }
                    }
                }


                //取摘要
                var summaryNode = li.SelectSingleNode("descendant::p[@class='content']");
                if (summaryNode != null)
                {
                    n.Summary = summaryNode.Value;


                    //取相同新闻链接

                    var sameNewsLinkNodes = summaryNode.Select("descendant::a");
                    if (sameNewsLinkNodes != null)
                    {
                        while (sameNewsLinkNodes.MoveNext())
                        {
                            var sameNewsLinkNode = sameNewsLinkNodes.Current;
                            if (sameNewsLinkNode.Value != null && sameNewsLinkNode.Value.Contains("相同新闻"))
                            {
                                n.SameNewsUrl = sameNewsLinkNode.GetAttribute("href", "");
                                break;
                            }
                        }
                    }

                }


                ////取来源
                //var sourceNode = li.SelectSingleNode("descendant::p[@class='fgray']");
                //if (sourceNode != null)
                //{
                //    string source = sourceNode.Value;
                //    source = System.Web.HttpUtility.HtmlDecode(source);
                //    var m = Regex.Match(source, @"来源：([\s\S]+)", RegexOptions.None);
                //    if (m.Success)
                //        n.Media = m.Groups[1].Value.Trim();
                //}




                news.Add(n);

            }

            //取下一页地址
            var nextPageNode = liNav.SelectSingleNode("//a[@title='下一页']");
            if (nextPageNode != null && nextPageNode.Value != null && nextPageNode.Value.Contains("下一页"))
            {
                nextPageUrl = nextPageNode.GetAttribute("href", "");
                if (!string.IsNullOrEmpty(nextPageUrl))
                    nextPageUrl = System.Web.HttpUtility.HtmlDecode(nextPageUrl);
            }
        }
    }
}
